//
//  AppDelegate+MXInitialPage.m
//  XinAnDaApp
//
//  Created by Lucas on 2017/8/2.
//  Copyright © 2017年 bluemobi. All rights reserved.
//

#import "AppDelegate+MXInitialPage.h"
#import "MXGuideScrollView.h"
#import "AvoidCrash.h"
#import <Bugly/Bugly.h>
#import "LoginViewController.h"
#import "MXBaseNavViewController.h"

#define BUGLY_APP_ID @"a949bcb15f"

@implementation AppDelegate (MXInitialPage)

- (void)configInitialPage
{
    [self _configBaseUIStyle];
    
    if (!self.window) {
        self.window = [[UIWindow alloc] init];
    }
    
    self.window.backgroundColor = [UIColor whiteColor];
    
//    self.window.rootViewController = MyVCCreateHelper.showMainTabBarNav;
    
    if (isLogin()) {
        MXMainTabBarViewController *root = [[MXMainTabBarViewController alloc]init];
        self.window.rootViewController = root;
    } else {
        LoginViewController *loginVC = [[LoginViewController alloc]initWithNibName:@"LoginViewController" bundle:nil];
        self.window.rootViewController = [[MXBaseNavViewController alloc]initWithRootViewController:loginVC];
    }
    
    [self.window makeKeyAndVisible];
    
    
//    //引导页在首次使用或新版本更新时显示
    if (MyLoginUserManager.isFirstStart) {
        [self _addGuideScrollView];
        [MyLoginUserManager restoreLogoutStateWithEnterLoginPage:NO];
    }
//    else if ([MyLoginUserManager isUserLoginWithEnterLoginPage:NO]) {
//        //登录状态判定
//        NSLog(@"登录状态");
//    } else {
//        NSLog(@"非登录状态");
//        [self.window setRootViewController:[[MXBaseNavViewController alloc]initWithRootViewController:[LoginViewController new]]];
//    }

    //提示HUD配置
    [self _configSVProgressHUD];
    
    //监听Bugs
//    [self _monitorBugs];
}


#pragma mark - private

/**
 *  描述: 配置基础UI样式
 */
- (void)_configBaseUIStyle
{
    
    [UIApplication sharedApplication].statusBarHidden = NO; //默认启动时状态栏是隐藏的，初始配置做下显示
    
    [UINavigationBar appearance].translucent = NO;
    [[UINavigationBar appearance] setBarTintColor:APP_COLOR_GREEN]; // 背景色
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];     // 按钮颜色
    [[UINavigationBar appearance] setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.0f],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    // 全局设置Cell分割线左对齐
    [[UITableView appearance] setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [[UITableView appearance] setSeparatorInset:UIEdgeInsetsZero];
    [[UITableViewCell appearance] setSeparatorInset:UIEdgeInsetsZero];
    if ([UITableView instancesRespondToSelector:@selector(setLayoutMargins:)]) {
        [[UITableView appearance] setLayoutMargins:UIEdgeInsetsZero];
        [[UITableViewCell appearance] setLayoutMargins:UIEdgeInsetsZero];
        [[UITableViewCell appearance] setPreservesSuperviewLayoutMargins:NO];
    }
}

/**
 *  描述: 添加引导页
 */
- (void)_addGuideScrollView
{
    MXGuideScrollView * scrollView = [[MXGuideScrollView alloc] init];
    [self.window.rootViewController.view addSubview:scrollView];
}


/**
 *  描述: 配置SVProgressHUD
 */
- (void)_configSVProgressHUD
{
    //设置SVProgressHUD
//    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD setMinimumDismissTimeInterval:1.5];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
}


/**
 *  描述: 监听Bugs
 */
- (void)_monitorBugs
{
    //初始化 AvoidCrash
    [AvoidCrash becomeEffective];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dealWithCrashMessage:) name:AvoidCrashNotification object:nil]; //监听通知:AvoidCrashNotification, 获取AvoidCrash捕获的崩溃日志的详细信息
    
    
    //初始化Bugly
    BuglyConfig * config = [[BuglyConfig alloc] init];
    config.reportLogLevel = BuglyLogLevelWarn; //设置自定义日志上报的级别，默认不上报自定义日志
    [Bugly startWithAppId:BUGLY_APP_ID config:config];
}


#pragma mark - selector action

- (void)dealWithCrashMessage:(NSNotification *)note
{
    //注意:所有的信息都在userInfo中
    //你可以在这里收集相应的崩溃信息进行相应的处理(比如传到自己服务器)
    NSLog(@"%@",note.userInfo);
    
    NSString * str1 = [NSString stringWithFormat:@"【AvoidCrash】崩溃名称：%@ | 导致崩溃代码所在方法：%@ | 导致崩溃的原因：%@",note.userInfo[@"errorName"],note.userInfo[@"errorPlace"],note.userInfo[@"errorReason"]];
    
    NSString * str2 = [NSString stringWithFormat:@"AvoidCrash默认防崩溃处理：%@ | NSException：%@ | 调用栈信息：%@",note.userInfo[@"defaultToDo"],note.userInfo[@"exception"],note.userInfo[@"callStackSymbols"]];
    
    NSException * exception = [NSException exceptionWithName:str1 reason:str2 userInfo:note.userInfo];
   
    [Bugly reportException:exception];
}

@end
